package vip.meeet.algorithm.str;


public class Num415 {

    public static void main(String[] args) {

//        System.out.println(addStrings("123","1234"));
//        System.out.println(addStrings("123","123"));
//        System.out.println(addStrings("1234","123"));
//        System.out.println(addStrings("1","9"));
        System.out.println(addStrings("9", "99"));
    }

    public static String addStrings(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        if (len1 >= len2) {
            return add(num1, num2);
        } else {
            return add(num2, num1);
        }
    }

    private static String add(String big, String small) {
        StringBuffer sb = new StringBuffer();
        int len1 = big.length();
        int len2 = small.length();
        int diff = len1 - len2;
        int rs;
        int add = 0;
        for (int i = len2 - 1; i >= 0; i--) {
            rs = big.charAt(i + diff) + small.charAt(i) - 96 + add;
            sb.append(rs % 10);
            add = rs / 10;

        }
        if (diff == 0) {
            if (add != 0) {
                sb.append(add);
            }
        } else {
            for (int i = diff - 1; i >= 0; i--) {
                rs = big.charAt(i) - 48 + add;
                sb.append(rs % 10);
                add = rs / 10;
            }
            if (add != 0) {
                sb.append(add);
            }
        }
        return sb.reverse().toString();
    }


}
